package com.mallcai.bigdata.ladon.dpl.mapper.peekdata.provider;

import com.mallcai.bigdata.ladon.dpl.entity.peekdata.ModelMeta;
import com.mallcai.bigdata.ladon.dpl.mapper.BaseSqlProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author gst
 * @date 2019-12-30 20:36
 */
public class ModelMetaSqlProvider extends BaseSqlProvider<ModelMeta> {

    public static final String TB_MODEL_META = "pd_model_meta";

    @Override
    public String getTableName() {
        return TB_MODEL_META;
    }

    @Override
    public Map<String, String> getInsertColumns() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("model_id", "#{modelId}");
        map.put("group_name", "#{groupName}");
        map.put("name", "#{name}");
        map.put("display", "#{display}");
        map.put("data_type", "#{dataType}");
        map.put("show_name", "#{showName}");
        map.put("tag_id", "#{tagId}");
        map.put("remark", "#{remark}");
        map.put("original_data_type", "#{originalDataType}");
        return map;
    }

    @Override
    public Map<String, String> getUpdateColumns() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("modelId", "model_id");
        map.put("groupName", "group_name");
        map.put("name", "name");
        map.put("display", "display");
        map.put("dataType", "data_type");
        map.put("showName", "show_name");
        map.put("tagId", "tag_id");
        map.put("remark", "remark");
        return map;
    }

    public String getByNames(@Param("modelId") Long modelId, @Param("names") List<String> names) {
        StringBuilder sb = new StringBuilder();
        sb.append(" archive = 0 ")
                .append(" AND model_id = #{modelId}");

        if (names != null && names.size() > 0) {
            sb.append(" AND name in (");
            int index = 0;
            for (String name : names) {
                if (index > 0) {
                    sb.append(",");
                }
                sb.append("'").append(name).append("'");
                index++;
            }
            sb.append(") ");
        }
        return new SQL() {
            {
                SELECT("*");
                FROM(getTableName());
                WHERE(sb.toString());
            }
        }.toString();
    }

    public String getByIds(@Param("ids") Collection<Long> ids) {
            StringBuilder sb = new StringBuilder();
            sb.append(" archive = 0 ");
            if (ids != null && ids.size() > 0) {
                sb.append(" AND id in (");
                sb.append(ids.stream().map(String::valueOf).collect(Collectors.joining(",")));
                sb.append(") ");
            }
            return new SQL() {
                {
                    SELECT("*");
                    FROM(getTableName());
                    WHERE(sb.toString());
                }
            }.toString();
    }
}
